package com.freemaster.sys.service.impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.freemaster.sys.entity.Role;
import com.freemaster.sys.mapper.RoleMapper;
import com.freemaster.sys.service.ResourceService;
import com.freemaster.sys.service.RoleService;

@Service
@Transactional(readOnly=false)
public class RoleServiceImpl implements RoleService{

	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	ResourceService resourceService;
	
	@Override
	public Set<String> findRoles(Long... roleIds) {
		Set<String> roles = new HashSet<String>();
		for (Long roleId : roleIds) {
			Role role =findOne(roleId);
			if(role !=null){
				roles.add(role.getRole());
			}
		}
		return roles;
	}

	@Override
	public Role findOne(Long roleId) {
		return roleMapper.findOne(roleId);
	}

	@Override
	public Set<String> findPermissions(Long[] roleIds) {
		Set<Long> resourceIds = new HashSet<Long>();
		for (Long roleId : roleIds) {
			Role role=findOne(roleId);
			if(role !=null){
				resourceIds.addAll(role.getResourceIds());
			}
		}
		return resourceService.findPermissions(resourceIds);
	}

	

 

}
